package com.orm.analysis.service;

import com.orm.analysis.dao.AddressDAO;
import com.orm.analysis.dao.CustomerDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/10/13
 * Time: 11:54 AM
 * To change this template use File | Settings | File Templates.
 */

public class AddressService  implements GenericService<Address> {


    private static Logger log = Logger.getLogger(AddressService.class);

    private String addressDataFielDir;

    private AddressDAO dao;

    public String getAddressDataFielDir() {
        return addressDataFielDir;
    }

    public void setAddressDataFielDir(String addressDataFielDir) {
        this.addressDataFielDir = addressDataFielDir;
    }

    public AddressDAO getDao() {
        return dao;
    }

    public void setDao(AddressDAO dao) {
        this.dao = dao;
    }

    public Address create(Address entity) {
        dao.save(entity);
        return entity;
    }


    public void delete(Object id) {

        dao.delete(id);
    }

    public Address update(Address entity) {
        dao.update(entity);
        return entity;
    }

    public List<Address> list(int page, int size) {
        return dao.list(page,size);
    }

    public Address getById(Address entity) {
        return dao.getById(entity.getAddressId());
    }

    public Address getById(Integer id) {
        return dao.getById(id);
    }

    public void executeImproter() throws ParseException,IOException {

        try{
        log.info("File  >>" + addressDataFielDir);
        BufferedReader br = null;

        br = new BufferedReader(new FileReader(addressDataFielDir));

        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        line = br.readLine();//Skip 1 line

        List<Address> addressList = new ArrayList<Address>();

        while (line != null) {
            log.info("File  >>" + line);
            //ADDRESS_ID ,STREET ,ZIPCODE ,ADDRESS_TYPE ,LAST_UPDATE

            Address address = new  Address();

            String[] data = line.split("[,]");



            address.setStreet(data[1]);
            address.setZipcode(data[2]);
            //HOME, OFFICE, SHIPPING,BILLING;
            Character character = data[3].charAt(0);

            switch (character) {
                case 'H':
                    address.setAddressType(AddressType.HOME);
                    break;
                case 'O':
                    address.setAddressType(AddressType.OFFICE);
                    break;
                case 'S':
                    address.setAddressType(AddressType.SHIPPING);
                    break;
                case 'B':
                    address.setAddressType(AddressType.BILLING);
                    break;
                default:
                    address.setAddressType(AddressType.HOME);
                    break;
            }


            address.setLastUpdate(GlobalConst.simpleDateFormat.parse(data[4].trim()));


            if (data[0].equals("1")) {
                InternationalAddress interAddressService = new InternationalAddress(address);
                interAddressService.setCountry(data[5].trim());
                interAddressService.setContinent(data[6].trim());
                addressList.add(interAddressService);
            } else {

                addressList.add(address);
            }


            line = br.readLine();



        }
           if(addressList.size()>0){

               dao.saveList(addressList);
           }


        br.close();

        }catch(Exception e ){
            e.printStackTrace();
        }

    }
}
